//
//  ShoufuViewController.m
//  EMMApp
//
//  Created by Mac on 16/7/1.
//  Copyright © 2016年 YiXun. All rights reserved.
//

#import "ShoufuViewController.h"

/** Cell
 *
 *  Cell导入头文件.h
 */
#import "MarketingCell.h"
#import "NewCustomersCell.h"
#import "ShoufuCell.h"

@interface ShoufuViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate> {
    NSMutableArray *selectedArr;//二级列表是否展开状态
    NSMutableArray *grouparr0;
    NSMutableArray *titleDataArray;
    NSMutableArray *grouparr1;
    NSString *xzIndex;
}

/**
 *  创建 - UITableView
 */
@property (nonatomic, strong) UITableView *knowDtailTableView;

/**
 *  数组
 */
@property (nonatomic, strong) NSMutableArray *seleArr;

/**
 *  首付输入框
 */
@property (nonatomic, strong) UITextField *sfTextField;

/**
 *  字段 - 首付
 */
@property (nonatomic, strong) NSString *sfString;

@end

@implementation ShoufuViewController

#pragma mark - 初始化
- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatNavigationController];
    self.view.backgroundColor = [UIColor whiteColor];
    selectedArr = [[NSMutableArray alloc] initWithObjects:@"0",nil];
    _knowDtailTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, EMM_WIDTH, EMM_HEIGHT-SafeAreaTopHeight-60) style:UITableViewStylePlain];
    _knowDtailTableView.delegate = self;
    _knowDtailTableView.dataSource = self;
    _knowDtailTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _knowDtailTableView.backgroundColor = [UIColor whiteColor];
    _knowDtailTableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_knowDtailTableView];
    [self initDataSource];
    [self creatDoneShowUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 导航创建
- (void)creatNavigationController {
    [self updateNavBar];
    self.navigationItem.title = @"首付比例";
    [self configLeftBtn];
}

#pragma mark - 生命周期函数调用
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

#pragma mark - 控件初始化创建
- (void)creatDoneShowUI {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, EMM_HEIGHT-SafeAreaTopHeight-60, EMM_WIDTH, 60)];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    doneBtn.frame = CGRectMake(20, 5, EMM_WIDTH-40, 50);
    [doneBtn setTitle:@"确定" forState:UIControlStateNormal];
    [doneBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    doneBtn.backgroundColor = [UIColor colorFromHexCode:@"#F87144"];
    [doneBtn addTarget:self action:@selector(doneBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    doneBtn.layer.masksToBounds = YES;
    doneBtn.layer.cornerRadius = 5;
    [view addSubview:doneBtn];
}

#pragma mark - 确定按钮触发事件调用方法
- (void)doneBtnClick:(UIButton *)btn {
    if ([NSString isStringEmpty2:_sfString] == YES) {
        [self showToast:@"首付金额未填写或者未选择"];
        return;
    }
    if (_shoufuBlock) {
        _shoufuBlock(_sfString);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 数据初始化
-(void)initDataSource {
    grouparr1 = [[NSMutableArray alloc] initWithObjects:@"1成",@"2成",@"2.5成",@"3成",@"4成",@"5成",@"6成",@"7成",@"8成",@"9成", nil];
    grouparr0 = [[NSMutableArray alloc] initWithObjects:
                 @"0.10",@"0.20",@"0.25",@"0.30",@"0.40",@"0.50",@"0.60",@"0.70",@"0.80",@"0.90", nil];
}

#pragma mark - UITableView代理
//返回几个表头
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

//每一个表头下返回几行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSString *string = [NSString stringWithFormat:@"%ld",section];
    if ([string isEqualToString:@"0"]) {
        return 1;
    }else {
        return grouparr1.count;
    }
    return 0;
}

//设置表头的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    }
    return 50;
}

//设置view，将替代titleForHeaderInSection方法
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, EMM_WIDTH, 50)];
    view.backgroundColor = [UIColor whiteColor];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 15, tableView.frame.size.width-20, 20)];
    titleLabel.text = @"或选择常用比例";
    [view addSubview:titleLabel];
    UIImageView *lineImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 49.5, EMM_WIDTH, 0.5)];
    lineImage.backgroundColor = [UIColor colorFromHexCode:@"#CCCCCC"];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, EMM_WIDTH, 50);
    button.tag = 100+section;
    //    [button addTarget:self action:@selector(doButton:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
    view.backgroundColor = [UIColor colorFromHexCode:@"#fff5e9"];
    UILabel *sLabel6 = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 2, 30)];
    sLabel6.backgroundColor = [UIColor colorFromHexCode:@"#ff7200"];
    [view addSubview:sLabel6];
    if (section == 0) {
        return nil;
    }
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *indexStr = [NSString stringWithFormat:@"%ld",indexPath.section];
    static NSString *CellIdentifier = @"MarketingCell";
    MarketingCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MarketingCell" owner:self options:nil] lastObject];
    }
    if ([indexStr isEqualToString:@"0"]) {
        static NSString *CellIdentifier = @"NewCustomersCell";
        NewCustomersCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"NewCustomersCell" owner:self options:nil] lastObject];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.nameLbael.text = @"首付金额";
        UIImageView *lineImage = [[UIImageView alloc] initWithFrame:CGRectMake(15, 48, EMM_WIDTH-30, 1)];
        lineImage.backgroundColor = [UIColor colorFromHexCode:@"f0f0f0"];
        [cell addSubview:lineImage];
        if (indexPath.row == 0) {
            cell.bitianLabel.hidden = YES;
            cell.customLabel.hidden = YES;
            cell.qjImageView.hidden = YES;
            cell.custTextField.hidden = YES;
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(EMM_WIDTH-30-15, 10, 30, 30)];
            label.textAlignment = NSTextAlignmentRight;
            label.text = @"元";
            label.font = [UIFont systemFontOfSize:14];
            [cell addSubview:label];
            
            _sfTextField = [[UITextField alloc] initWithFrame:CGRectMake(EMM_WIDTH-200-50, 10, 200, 30)];
            _sfTextField.delegate = self;
            _sfTextField.returnKeyType =UIReturnKeyDone;
            _sfTextField.textAlignment = NSTextAlignmentRight;
            _sfTextField.font = [UIFont systemFontOfSize:15];
            _sfTextField.keyboardType = UIKeyboardTypeDecimalPad;
            [cell addSubview:_sfTextField];
            if ([NSString isStringEmpty2:_sfString] == YES) {
                _sfTextField.placeholder = @"请输入";
            }else {
                _sfTextField.text = _sfString;
            }
        }
        return cell;
    }else {
        static NSString *CellIdentifier = @"ShoufuCell";
        ShoufuCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"ShoufuCell" owner:self options:nil] lastObject];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UIImageView *lineImage = [[UIImageView alloc] initWithFrame:CGRectMake(15, 48, EMM_WIDTH-30, 1)];
        lineImage.backgroundColor = [UIColor colorFromHexCode:@"f0f0f0"];
        [cell addSubview:lineImage];
        cell.nameLabel.text = [grouparr1 objectAtIndex:indexPath.row];
        NSString *string = [NSString stringWithFormat:@"%ld",indexPath.row];
        if ([string isEqualToString:xzIndex]) {
            cell.xzImageView.image = [UIImage imageNamed:@"choose"];
        }else {
            cell.xzImageView.image = [UIImage imageNamed:@"choose_no"];
        }
        return cell;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *indexStr = [NSString stringWithFormat:@"%ld",indexPath.section];
    if ([indexStr isEqualToString:@"0"]) {
        
    }else {
        xzIndex = [NSString stringWithFormat:@"%ld",indexPath.row];
        _sfString = [NSString stringWithFormat:@"%.2f",[[grouparr0 objectAtIndex:indexPath.row] floatValue] * [_totalAmount floatValue]];
        [_knowDtailTableView reloadData];
    }
}

#pragma mark - 二级展开按钮触发方法
-(void)doButton:(UIButton *)sender {
    NSString *string = [NSString stringWithFormat:@"%ld",sender.tag-100];
    //数组selectedArr里面存的数据和表头想对应，方便以后做比较
    if ([selectedArr containsObject:string]) {
        [selectedArr removeObject:string];
    } else  {
        [selectedArr addObject:string];
    }
    [_knowDtailTableView reloadData];
}

#pragma mark - UITextField代理方法
-(void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField == _sfTextField) {
        _sfString = textField.text;
        int jg = [_totalAmount intValue];
        int jg1 = [_sfString intValue];
        if ([NSString isStringEmpty2:_sfString] == YES) {
            [self showToast:@"首付未输入"];
            return;
        }
        if (jg1 > jg) {
            [self showToast:@"输入的金额不能大于总金额"];
            return;
        }
    }
}

@end
